const Cc = Components.classes;
const Ci = Components.interfaces;

var FirepixEditor = {
  
  initCache: function() {
    if(this.initialized) {
      return;
    }
    
    this.initialized = true;
    this.editorNameElem = document.getElementById("editor-name");
    this.editorPathElem = document.getElementById("editor-path");
    this.editorListElem = document.getElementById("editor-list");
    this.addEditorElem = document.getElementById("add-editor-btn");
  },
  
  onLoad: function() {
    this.initCache();
    
    var editors = window.arguments[0].editors;
    for(var i in editors) {
      this.addEditorToList(editors[i].name, editors[i].path);
    }
  },
  
  onSelectClick: function() {
    var nsIFilePicker = Ci.nsIFilePicker;
    var filePicker = Cc["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
    
    filePicker.init(window, "Select Editor", nsIFilePicker.modeOpen);
    filePicker.appendFilters(nsIFilePicker.filterApps);
    
    var result = filePicker.show();
    if (result == nsIFilePicker.returnOK) {
      var chosenFile = filePicker.file;
      this.editorPathElem.value = chosenFile.path;
      
      // try to automatically set a name for the editor using the filename
      var extIdx = chosenFile.leafName.lastIndexOf(".");
      if(extIdx > 0) {
        var suggestedName = chosenFile.leafName.substr(0, extIdx);
        suggestedName = suggestedName.replace(/(-|_)/g, " ");
        suggestedName = suggestedName.replace(/(^[a-z])|( [a-z])/g, function(str) {
          return str.toUpperCase();
        });
        this.editorNameElem.value = suggestedName;
      }
      
      this.onInput();
    }
  },
  
  onAddClick: function() {
    // store the values from the inputs
    var editorName = this.editorNameElem.value;
    var editorPath = this.editorPathElem.value;
    
    // clear out the inputs
    this.editorNameElem.value = "";
    this.editorPathElem.value = "";
    this.addEditorElem.setAttribute("disabled", true);
    
    this.addEditorToList(editorName, editorPath);
    window.arguments[0].editorsChanged = true;
  },
  
  addEditorToList: function(name, path) {
    // create the appropriate elements
    var newListElem = document.createElement("listitem");
    var listNameElem = document.createElement("listcell");
    listNameElem.setAttribute("label", name);
    var listPathElem = document.createElement("listcell");
    listPathElem.setAttribute("label", path);
    
    // add them to the listbox
    newListElem.appendChild(listNameElem);
    newListElem.appendChild(listPathElem);
    this.editorListElem.appendChild(newListElem);
  },
  
  onRemoveClick: function() {
    var listItems = document.getElementsByTagName("listitem");
    for(var i in listItems) {
      if(listItems[i].getAttribute("selected")) {
        this.editorListElem.removeChild(listItems[i]);
        break;
      }
    }
    window.arguments[0].editorsChanged = true;
  },
  
  onInput: function() {
    var inputsAreComplete = this.editorNameElem.value.length > 0
                         && this.editorPathElem.value.length > 0;
    
    this.addEditorElem .setAttribute("disabled", !inputsAreComplete);
  },
  
  onSaveClick: function() {
    var params = window.arguments[0];
    params.editors = [];
    var listItems = document.getElementsByTagName("listitem");
    for(var i in listItems) {
      var itemElem = listItems[i];
      if(itemElem) {
        var editor = {};
        
        var nameElem = itemElem.firstChild;
        if(nameElem) {
          editor.name = nameElem.getAttribute("label");
          editor.path = nameElem.nextSibling.getAttribute("label");
          params.editors.push(editor);
        }
      }
    }
    
    return true;
  },
  
  onCancelClick: function() {
    window.arguments[0].editorsChanged = false;
    return true;
  }
};